package mvc.controller.filter;

import mvc.model.pojos.PictureBean;
import mvc.model.pojos.ProductBean;
import mvc.model.pojos.RomBean;
import mvc.model.service.ProductService;
import mvc.model.service.impl.ProductServiceImpl;
import mvc.model.vo.ColorVO;
import mvc.model.vo.Color_Path;
import mvc.model.vo.HeadProductVO;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@WebFilter(filterName = "AMFilter", urlPatterns = "/maindetail.jsp")
public class AMFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        String p = request.getParameter("pid");
        if (p == null) {
            p = "1";
        }
        //right界面
        int pid = Integer.parseInt(p);
        ProductService productService = new ProductServiceImpl();

        ProductBean productBean = productService.queryById(pid);

        String pname = productBean.getPname();

        List<HeadProductVO> pros = productService.findSome().stream().limit(6).collect(Collectors.toList());
        List<ColorVO> colors = productService.getColor(pname);
        List<RomBean> roms = productService.getRom(pname);


        request.getSession().setAttribute("pros", pros);
        request.getSession().setAttribute("colors", colors);
        request.getSession().setAttribute("roms", roms);
        request.getSession().setAttribute("tp", productBean);


        //left界面
        List<PictureBean> picture = productService.getPicture(pid);
        request.getSession().setAttribute("pictures", picture);

        System.out.println("am");
        chain.doFilter(request, response);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
